const BaseController = require('./base');

class MeetingController extends BaseController {
  async addMeeting() {
    const { ctx } = this;
    ctx.validate({
      name: { type: 'string' },
    });
    const meetingInfo = ctx.request.body;
    const result = await ctx.service.meeting.addMeeting(meetingInfo);
    if (result) {
      return this.success('添加成功');
    }
    return this.fail('添加失败');

  }

  async delMeeting() {
    const { ctx } = this;
    const ids = ctx.request.body;
    if (!ids || !(ids instanceof Array)) {
      this.fail('参数错误');
      return;
    }
    const res = await ctx.service.meeting.delMeeting(ids);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async getMeetings() {
    const { ctx } = this;
    const { id, pageNum = 1, pageSize = 10, name = '' } = ctx.query;
    if (id) {
      const res = await ctx.service.meeting.getMeeting(id);
      if (res) {
        return this.success(res);
      }
      return this.fail('获取失败');
    }
    const result = await ctx.service.meeting.getMeetings(
      Number(pageNum),
      Number(pageSize),
      name
    );
    if (result) {
      return this.success(result);
    }
    return this.fail('获取失败');
  }

  async updateMeeting() {
    const { ctx } = this;
    ctx.validate({
      id: { type: 'string' },
      name: { type: 'string' },
    });
    const meetingInfo = ctx.request.body;
    const result = await ctx.service.meeting.updateMeeting(meetingInfo);
    if (result) {
      return this.success('修改成功');
    }
    return this.fail('修改失败');

  }

  async addMeetingUsers() {
    const { ctx } = this;
    ctx.validate({
      meetingId: { type: 'string' },
      userIds: { type: 'array' },
    });
    const { meetingId, userIds } = ctx.request.body;
    const result = await ctx.service.meeting.addMeetingUsers(meetingId, userIds);
    if (result.state) {
      return this.success('添加成功');
    }
    return this.fail(result.msg);
  }

  async delMeetingUsers() {
    const { ctx } = this;
    ctx.validate({
      meetingId: { type: 'string' },
      userIds: { type: 'array' },
    });
    const { meetingId, userIds } = ctx.request.body;
    const result = await ctx.service.meeting.delMeetingUsers(meetingId, userIds);
    if (result) {
      return this.success('删除成功');
    }
    return this.fail('删除失败');
  }

  async sendEmail() {
    const { ctx } = this;
    ctx.validate({
      meetingId: { type: 'string' },
      userId: { type: 'string' },
    });
    const { meetingId, userId } = ctx.request.body;
    const result = await ctx.service.meeting.sendEmail(meetingId, userId);
    if (result.state) {
      return this.success({
        type: 'success',
        msg: result.msg,
      });
    }
    return this.success({
      type: 'error',
      msg: result.msg,
    });
  }

  async updateInterViewTime() {
    const { ctx } = this;
    ctx.validate({
      interviewId: { type: 'string' },
      begin: { type: 'string' },
      end: { type: 'string' },
    });
    const { interviewId, begin, end, interviewer_id } = ctx.request.body;
    const result = await ctx.service.meeting.updateInterViewTime({ interviewId, begin, end, interviewer_id });
    if (result) {
      return this.success('更新成功');
    }
    return this.fail('更新失败');
  }

  async getInterView() {
    const { ctx } = this;
    const { userId, day } = ctx.query;
    const result = await ctx.service.meeting.getInterView(userId, day);
    if (result) {
      return this.success(result);
    }
    return this.fail('获取失败');
  }
}

module.exports = MeetingController;
